<?php
/**
 * 营销活动
 *
 * @author maolvxiansheng 2018年9月13日 08点24分
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class marketing_activityControl extends mobileHomeControl {

    public function __construct() {
        parent::__construct();
        $this->model_marketing = Model('marketing_activity');
    }

    /**
     * 获得营销活动列表
     * @return json
     */
    public function get_marketing_activity_listOp()
    {
        $model_marketing = $this->model_marketing;
        $marketing_activity_type = isset($_GET['marketing_activity_type']) ? (int)$_GET['marketing_activity_type'] : 0;
        $map = array(
            'marketing_activity_state'  => $model_marketing::ACTIVITY_OPEN,
        );
        // 营销活动类型   -- 只检索 新人专场/限时折扣（全商城）
        $map['marketing_activity_type'] = ($marketing_activity_type && in_array($marketing_activity_type, array($model_marketing::ACTIVITY_TYPE_NEWBIE, $model_marketing::ACTIVITY_TYPE_SECAKILL))) ? array('eq', $marketing_activity_type) : array('in', array($model_marketing::ACTIVITY_TYPE_NEWBIE, $model_marketing::ACTIVITY_TYPE_SECAKILL));
        $order = 'marketing_activity.marketing_activity_sort ASC';
        $demand_goods = isset($_GET['demand_goods']) ? (bool)$_GET['demand_goods'] : false;
        $limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 0;
        $member_id = $this->getMemberIdIfExists();
        $result = $model_marketing->getMarketingActivityList($map, $order, $demand_goods, $limit, $member_id);
        if ($result === false) {
            output_error($model_marketing->getErrorInfo());
        }
        output_data($result);
    }

    /**
     * 获取营销活动商品列表
     */
    public function get_activity_goodsOp()
    {
        $model_marketing = $this->model_marketing;
        $activity_id = isset($_GET['activity_id']) ? (int)$_GET['activity_id'] : 0;

        if (!$activity_id) {
            $activity_type_map = [
                'newbie' => $model_marketing::ACTIVITY_TYPE_NEWBIE,
                'secakill' => $model_marketing::ACTIVITY_TYPE_SECAKILL
            ];
            $activity_type = isset($_GET['activity_type']) && isset($activity_type_map[$_GET['activity_type']]) ? $activity_type_map[$_GET['activity_type']] : null;
            if (is_null($activity_type)) {
                output_error('请传入活动ID，或活动类型');
            }

            //获取该活动类型下，活动类型为开启，且是最新的一条数据
            $map_newest = array(
                'marketing_activity_type' => $activity_type,
                'marketing_activity_start_date' => array('lt', TIMESTAMP),
                'marketing_activity_end_date' => array('gt', TIMESTAMP),
                'marketing_activity_state' => $model_marketing::ACTIVITY_OPEN
            );
            $newest_data = $model_marketing->table('marketing_activity')->field('marketing_activity_id')->where($map_newest)->order('create_time DESC')->find();
            $activity_id = isset($newest_data['marketing_activity_id']) ? (int)$newest_data['marketing_activity_id'] : 0;
        }

        if (!$activity_id) {    //活动id 为0，直接返回空数组
            output_data(array(), array('goods_list' => array()));
        }
        //获取活动详情
        $activity_data = $model_marketing->getOneById($activity_id);
        if (empty($activity_data)) {
            output_error('不存在该进行中活动');
        }
        //活动状态是否为开启
        if ($activity_data['marketing_activity_state'] != $model_marketing::ACTIVITY_OPEN) {
            output_error('活动已关闭');
        }
        //判断活动是否在活动中
        if (($activity_data['marketing_activity_start_date'] >= TIMESTAMP) || ($activity_data['marketing_activity_end_date'] <= TIMESTAMP)) {
            $msg = '该活动开启时间为：' . date('Y-m-d H:i:s', $activity_data['marketing_activity_start_date']) . ' -- ' . date('Y-m-d H:i:s', $activity_data['marketing_activity_end_date']);
            output_error($msg);   
        }
        //确定活动类型模型
        $model_name = $model_marketing->activityTypeShiftModel($activity_data['marketing_activity_type']);
        if (!$model_name) {
            output_error('营销类型错误');
        }
        $model_name = Model($model_name);
        $activity_goods = array();  //活动下属商品
        //判断模型是否正确
        if (method_exists($model_name, '_correctModel')) {
            //获取活动下属商品
            $temporary_data = $model_name->getListDataPass($activity_id);
            $member_id = $this->getMemberIdIfExists();  //会员id
            array_walk($temporary_data, function($item, $keys) use(&$activity_goods, $member_id, $model_name) {
                $buy_jurisdiction = true;    //是否具有权限
                //是否存在会员id，若存在检查是否具有权限
                if ($member_id) {
                    $buy_jurisdiction = $model_name->checkMemberBuyJurisdiction($member_id, $item);
                    $buy_jurisdiction = ($buy_jurisdiction === true) ? false : true;
                }
                //商品图片
                $item['goods_image_url'] = cthumb($item['goods_image'], 360, $item['store_id']);
                //折扣
                $item['discount'] = number_format(($item['promotion_price'] / $item['goods_price']), 2, '.', '');
                if ($buy_jurisdiction) $activity_goods[$keys] = $item;
            });
        }
        output_data($activity_data, array('goods_list' => $activity_goods));
    }



}
